﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

namespace Nirvana
{
    [CustomEditor(typeof(SpriteImportRule), true)]
    public class SpriteImportRuleEditor : Editor
    {
        private SerializedProperty includeRegexs;
        private SerializedProperty excludeRegexs;
        private SerializedProperty textureType;
        private SerializedProperty wrapMode;
		private SerializedProperty filterMode;
		private SerializedProperty anisoLevel;
		private SerializedProperty spriteImportMode;
		private SerializedProperty spritePixelsPerUnit;
		private SerializedProperty spritePivot;
		private SerializedProperty spriteBorder;
		private SerializedProperty spritePackingTag;
		private SerializedProperty sRGBTexture;
		private SerializedProperty alphaSource;
		private SerializedProperty alphaIsTransparency;
		private SerializedProperty isReadable;
		private SerializedProperty mipmapEnabled;
		private SerializedProperty androidTextureFormatNoAlpha;
		private SerializedProperty androidTextureFormatAlpha;
		private SerializedProperty iosTextureFormatNoAlpha, iosTextureFormatAlpha;
        private SerializedProperty webGLTextureFormat;
        private SerializedProperty maxTextureSize;
		private SerializedProperty compressionQuality;

		public override void OnInspectorGUI()
        {
            base.serializedObject.Update();

            GUILayout.Label("匹配规则", GUITheme.LabelTitleStyle);
            GUILayoutEx.BeginContents(GUI.skin.window);
            GUI.color = Color.yellow;
            EditorGUILayout.HelpBox("注意：规则文件只会检测同一父目录或同一级目录下的资源。\n\n" +
                "正则例子：1.匹配非空字符：\\S , 2.任意字符：. ", MessageType.None);
            GUI.color = Color.white;
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.includeRegexs);
            EditorGUILayout.PropertyField(this.excludeRegexs);

            GUILayoutEx.EndContents();
            EditorGUILayout.Space();

            GUILayout.Label("Sprite规则", GUITheme.LabelTitleStyle);
            GUILayoutEx.BeginContents(GUI.skin.window);
            EditorGUILayout.PrefixLabel("Common");
            EditorGUILayout.PropertyField(this.textureType);
            EditorGUILayout.PropertyField(this.wrapMode);
            EditorGUILayout.PropertyField(this.filterMode);
            EditorGUILayout.PropertyField(this.anisoLevel);
            EditorGUILayout.PropertyField(this.spriteImportMode);
            EditorGUILayout.PropertyField(this.spritePixelsPerUnit);
            EditorGUILayout.PropertyField(this.spritePivot);
            EditorGUILayout.PropertyField(this.spriteBorder);

            EditorGUILayout.Space();
            EditorGUILayout.PrefixLabel("Advanced");
            EditorGUILayout.PropertyField(this.sRGBTexture);
            EditorGUILayout.PropertyField(this.alphaSource);
            EditorGUILayout.PropertyField(this.alphaIsTransparency);
            EditorGUILayout.PropertyField(this.isReadable);
            EditorGUILayout.PropertyField(this.mipmapEnabled);

            EditorGUILayout.Space();

            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.PrefixLabel("Packing Tag");
            EditorGUILayout.HelpBox("设置Tag，默认是不开启的，需要则手动开启，Unity新版以SpriteAtlas图集方式打包，已不需要设置Tag方式，[[后续这个功能修改为AdressAble的自动命名标签功能]]\n" +
                "说明：\n" +
                "{filename}：tag记录文件名，{firstdir}：配置表下的第一个子目录开始，{seconddir}，{thirddir}。\n" +
                "一般设置三级目录Tag做图集标签即可，最多就记录到三级目录。例子：{firstdir}/{seconddir}/{thirddir}。\n", MessageType.None);
            EditorGUILayout.PropertyField(this.spritePackingTag);
            GUILayoutEx.EndContents();


            EditorGUILayout.Space();
            Color32 backgroundColor = GUI.backgroundColor;
            GUILayoutEx.BeginContents(GUI.skin.box);
            GUI.backgroundColor = Color.yellow;
            EditorGUILayout.PrefixLabel("Android Texture Format");
            EditorGUILayout.HelpBox("针对Android平台Unity还实现了一套Crunched（紧缩）压缩方式，比如RGBA Crunched ETC2，先ETC2先压缩，然后在Crunched压缩，运行时在额外解压缩，所以大小更下，加载更快。\n" +
                "建议选择：ETC_RGB4 Crunched 和ETC_RGBA8 Crunched\n", MessageType.None);
            EditorGUILayout.PropertyField(this.androidTextureFormatNoAlpha,new GUIContent("NoAlpha"));
            EditorGUILayout.PropertyField(this.androidTextureFormatAlpha,new GUIContent("Alpha"));
            GUILayoutEx.EndContents();
            EditorGUILayout.Space();

            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.PrefixLabel("IOS Texture Format");
            EditorGUILayout.HelpBox("从iOS9(A8架构)开始支持ASTC压缩格式 ，相对于PVRTC2/4而言，ASTC拥有更高的质量，且占用更少的内存。\n" +
            "建议选择：无Alpha的ASTC 6x6 ，带Alpha的ASTC 5x5,有更高要求的贴图（比如特效、UI），可以设置压缩格式为ASTC 4x4\n", MessageType.None);
            EditorGUILayout.PropertyField(this.iosTextureFormatNoAlpha, new GUIContent("NoAlpha"));
            EditorGUILayout.PropertyField(this.iosTextureFormatAlpha, new GUIContent("Alpha"));
            GUILayoutEx.EndContents();

            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.PrefixLabel("WebGL Texture Format");
            EditorGUILayout.HelpBox("从unity2021,webgl开始支持ASTC压缩格式 ，ASTC拥有更高的质量，且占用更少的内存。\n", MessageType.None);
            EditorGUILayout.PropertyField(this.webGLTextureFormat, new GUIContent("WebGL"));
            GUILayoutEx.EndContents();

            GUI.backgroundColor = backgroundColor;

            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.maxTextureSize);
            EditorGUILayout.PropertyField(this.compressionQuality, new GUIContent("compressionQuality(品质0-100)"));
            EditorGUILayout.Space(10f);
            GUILayoutEx.EndContents();
     
            base.serializedObject.ApplyModifiedProperties();
        }

        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;
            this.includeRegexs = serializedObject.FindProperty("includeRegexs");
            this.excludeRegexs = serializedObject.FindProperty("excludeRegexs");
            this.textureType = serializedObject.FindProperty("textureType");
            this.wrapMode = serializedObject.FindProperty("wrapMode");
            this.filterMode = serializedObject.FindProperty("filterMode");
            this.anisoLevel = serializedObject.FindProperty("anisoLevel");
            this.spriteImportMode = serializedObject.FindProperty("spriteImportMode");
            this.spritePixelsPerUnit = serializedObject.FindProperty("spritePixelsPerUnit");
            this.spritePivot = serializedObject.FindProperty("spritePivot");
            this.spriteBorder = serializedObject.FindProperty("spriteBorder");
            this.spritePackingTag = serializedObject.FindProperty("spritePackingTag");
            this.sRGBTexture = serializedObject.FindProperty("sRGBTexture");
            this.alphaSource = serializedObject.FindProperty("alphaSource");
            this.alphaIsTransparency = serializedObject.FindProperty("alphaIsTransparency");
            this.isReadable = serializedObject.FindProperty("isReadable");
            this.mipmapEnabled = serializedObject.FindProperty("mipmapEnabled");
            this.androidTextureFormatNoAlpha = serializedObject.FindProperty("androidTextureFormatNoAlpha");
            this.androidTextureFormatAlpha = serializedObject.FindProperty("androidTextureFormatAlpha");
            this.iosTextureFormatNoAlpha = serializedObject.FindProperty("iosTextureFormatNoAlpha");
            this.iosTextureFormatAlpha = serializedObject.FindProperty("iosTextureFormatAlpha");
            this.webGLTextureFormat = serializedObject.FindProperty("webGLTextureFormat");
            this.maxTextureSize = serializedObject.FindProperty("maxTextureSize");
            this.compressionQuality = serializedObject.FindProperty("compressionQuality");

        }

    }
}

